package com.gxuwz.leave.dao;

import com.gxuwz.leave.entity.Leave;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LeaveDaoImpl implements LeaveDao{
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(Leave entity) {
        String sql = "insert into sys_leave(leaveID, courseID, reason, daynum, stuNo, applytime, status, audittime, opinion) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            paramList.add(entity.getLeaveID());
            paramList.add(entity.getCourseID());
            paramList.add(entity.getReason());
            paramList.add(entity.getDaynum());
            paramList.add(entity.getStuNo());
            paramList.add(entity.getApplytime());
            paramList.add(entity.getStatus());
            paramList.add(entity.getAudittime());
            paramList.add(entity.getOpinion());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public void delete(String leaveID) {
        String sql = "delete from sys_leave where leaveID = ? ";
        if(!"".equals(leaveID)){
            jdbcTemplate.update(sql, leaveID);
        }
    }

    @Override
    public void update(Leave entity) {
        String sql = "update sys_leave set ";
        List<Object> paramList = new ArrayList<>();
        System.out.println("要修改的实体类的ID为："+ entity.getLeaveID());
        System.out.println("要修改的实体类的状态为："+ entity.getStatus());
        System.out.println("要修改的实体类的意见为为："+ entity.getOpinion());
        if(entity ==null){
            return;
        }else{
            if(entity.getCourseID()!=null && !"".equals(entity.getCourseID())){
                sql+="courseID = ?, ";
                paramList.add(entity.getCourseID());
            }
            if(entity.getReason()!=null && !"".equals(entity.getReason())){
                sql+="reason = ?, ";
                paramList.add(entity.getReason());
            }
            if(entity.getDaynum()!=null && !"".equals(entity.getDaynum())){
                sql+="daynum = ?, ";
                paramList.add(entity.getDaynum());
            }
            if(entity.getStuNo()!=null && !"".equals(entity.getStuNo())){
                sql+="stuNo = ?, ";
                paramList.add(entity.getStuNo());
            }
            if(entity.getApplytime()!=null && !"".equals(entity.getApplytime())){
                sql+="applytime = ?, ";
                paramList.add(entity.getApplytime());
            }
            if(entity.getStatus()!=null && !"".equals(entity.getStatus())){
                sql+="status = ?, ";
                paramList.add(entity.getStatus());
            }
            if(entity.getAudittime()!=null && !"".equals(entity.getAudittime())){
                sql+="audittime = ?, ";
                paramList.add(entity.getAudittime());
            }
            if(entity.getOpinion()!=null && !"".equals(entity.getOpinion())){
                sql+="opinion = ?, ";
                paramList.add(entity.getOpinion());
            }
            // 移除多余的逗号和空格
            sql = sql.substring(0, sql.length() - 2);
            sql+="where leaveID = ?";
            paramList.add(entity.getLeaveID());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public List<Leave> query(Leave entity) {
        String sql = "select * from sys_leave where 1=1 ";
        List<Leave> leaves = new ArrayList<>();
        List<Object> paramList = new ArrayList<>();
        if(entity!=null){
            if(entity.getStuNo()!=null && !"".equals(entity.getStuNo())){
                sql+="and stuNo = ?";
                paramList.add(entity.getStuNo());
            }
            if(entity.getCourseID()!=null && !"".equals(entity.getCourseID())){
                sql+="and courseID = ?";
                paramList.add(entity.getCourseID());
            }
            if(entity.getStatus()!=null && !"".equals(entity.getStatus())){
                sql +=" and status = ?";
                paramList.add(entity.getStatus());
            }
        }
        leaves = jdbcTemplate.query(sql, new RowMapper<Leave>() {
            @Override
            public Leave mapRow(ResultSet resultSet, int i) throws SQLException {
                Leave leave = new Leave();
                leave.setLeaveID(resultSet.getString("leaveID"));
                leave.setCourseID(resultSet.getString("courseID"));
                leave.setReason(resultSet.getString("reason"));
                leave.setDaynum(resultSet.getInt("daynum"));
                leave.setStuNo(resultSet.getString("stuNo"));
                leave.setApplytime(resultSet.getTimestamp("applytime"));
                leave.setStatus(resultSet.getString("status"));
                leave.setAudittime(resultSet.getTimestamp("audittime"));
                leave.setOpinion(resultSet.getString("opinion"));
                return leave;
            }
        }, paramList.toArray());
        return leaves;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
}
